﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;
using System.IO;

namespace Interface.Posts
{
    public partial class EditPost : System.Web.UI.Page
    {        
        string stringID;
        DataConnection dc = new DataConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["AdminSession"] == null)
            {
                Response.Redirect("~/Home.aspx");
            }  
            if (Page.IsPostBack == false)
            {
                getQueryString();
            }
        }

        public void getQueryString()
        {
            if (Request.QueryString["ID"] != null)
            {
                stringID = Request.QueryString["ID"].ToString();
                BindPost(stringID);
            }
        }

        protected void BindPost(string ID)
        {
            string sqlStr = "Select * from Post where PostID='" + ID + "'";
            DataTable dt = dc.getData(sqlStr);            
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                int PostID = Convert.ToInt32(row["PostID"].ToString());                               
                string title = row["Title"].ToString();
                string shortDes = row["ShortDescript"].ToString();
                string fullDes = row["Descript"].ToString();
                string image = row["ImageSource"].ToString();
                string status = row["PostStatus"].ToString();
                string lastUpdate = row["LastUpdate"].ToString();

                hdfID.Value = PostID.ToString();
                txtTitle.Text = title;
                txtShort.Text = shortDes;
                txtFull.Text = fullDes;
                lblCurrFile.Text = image;
                ddlStatus.Text = status;
                
                         
            }
        }
        

        protected void imbCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("PostDetails.aspx?ID=" + hdfID.Value);      
        }

        protected void imbUpdatePost_Click(object sender, ImageClickEventArgs e)
        {
            if (txtTitle.Text == string.Empty)
            {
                lblMs.Text = "Bạn chưa nhập tiêu đề";
                return;
            }
            if (txtShort.Text == string.Empty)
            {
                lblMs.Text = "Bạn chưa nhập nội dung ngắn";
                return;
            }

            if (txtFull.Text == string.Empty)
            {
                lblMs.Text = "Bạn chưa nhập nội dung đầy đủ";
                return;
            }
            
            string postID = hdfID.Value;
            string title = txtTitle.Text;
            string shortDes = txtShort.Text;
            string fullDes = txtFull.Text;
            HttpPostedFile postfile = fuPost.PostedFile;
            string fileEx = Path.GetExtension(postfile.FileName);
            string filename = Path.GetFileName(postfile.FileName);
            string image = lblCurrFile.Text;
            string status = ddlStatus.SelectedItem.Text;
            string lastUpdate = DateTime.Now.ToString();
            string sqlUpdate = "";

            if (filename != string.Empty)
            {
                if (fileEx == ".jpg" || fileEx == ".gif" || fileEx == ".png" || fileEx == ".bmp")
                {
                    postfile.SaveAs(Server.MapPath("~/Images/Post/" + filename));
                    sqlUpdate = "Update Post set Title=N'" + title + "', ShortDescript=N'" + shortDes + "', Descript= N'" + fullDes + "', ImageSource= '" + filename + "', LastUpdate='" + lastUpdate + "', PostStatus=N'" + status + "' where PostID='" + postID + "'";
                }
                else
                {
                    lblMs.Text = "Bạn phải nhập hình ảnh có đuôi .jpg, .gif, .png or .bmp";
                    return;
                }
            }
            else
            {
                sqlUpdate = "Update Post set Title=N'" + title + "', ShortDescript= N'" + shortDes + "', Descript=N'" + fullDes + "', ImageSource='" + image + "', LastUpdate='" + lastUpdate + "', PostStatus=N'" + status + "' where PostID='" + postID + "'";
            }
            dc.ExecNonQuery(sqlUpdate);
            Response.Redirect("PostDetails.aspx?ID=" + postID);                                   
        }
    }
}